home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / fileutil.13 / fileutil / fileutils-3.13 / lib / strndup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-03  |  1.2 KB  |  50 lines

  1. /* Copyright (C) 1996 Free Software Foundation, Inc.
  2.  
  3. NOTE: The canonical source of this file is maintained with the GNU C Library.
  4. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
  5.  
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #ifdef HAVE_CONFIG_H
  21. # include "config.h"
  22. #endif
  23.  
  24. #include <stdio.h>
  25. #include <sys/types.h>
  26.  
  27. #ifdef STDC_HEADERS
  28. # include <string.h>
  29. # include <stdlib.h>
  30. #else
  31. char *malloc ();
  32. #endif
  33.  
  34. /* Duplicate S, returning an identical malloc'd string.  */
  35. char *
  36. strndup (s, n)
  37.      const char *s;
  38.      size_t n;
  39. {
  40.   char *new = malloc (n + 1);
  41.  
  42.   if (new == NULL)
  43.     return NULL;
  44.  
  45.   memcpy (new, s, n);
  46.   new[n] = '\0';
  47.  
  48.   return new;
  49. }
  50.